﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.Communication.Items;
using System;
using System.Threading;
using System.Threading.Tasks;
using Utility;

namespace Proxy.Handle.Control.Machine
{

    public class MachineHandle
    {
        SearchInfoHandle Handle_Search;
        ModifyInfoHandle Handle_Modify;
        PlcHandle Handle_Plc;
        public MachineHandle()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Plc = new PlcHandle();

            Task.Factory.StartNew(() => Position_Transport("Section_M1L"));
            Task.Factory.StartNew(() => Position_Transport("Section_M1R"));
            Task.Factory.StartNew(() => Position_Transport("Section_M2"));
        }


        public void Position_Transport(string Location)
        {
            Item Item_OrderState = null;
            Item Item_OrderId = null;
            Item Item_Reference = null;
            string Section = "";
            View_TransOnlineRecordsDetail view_TransOnlineRecords = new View_TransOnlineRecordsDetail();
            do
            {
                try
                {
                Start:
                    if (Globle.isInfeedAreaActive && !Globle.isRunEmptyActive)//进料功能开启但RunEmpty没激活，正常走
                    {
                        Thread.Sleep(2000);
                        switch (Location)
                        {
                            case "Section_M1":
                                //Item_Reference = PlcProvider.Item_SM1L_P1_In_Reference;
                                break;
                            case "Section_M2":
                                //Item_Reference = PlcProvider.Item_SM2_P1_In_Reference;
                                break;
                            case "Section_M3":
                                //Item_Reference = PlcProvider.Item_SM2_P1_In_Reference;
                                break;
                            default:
                                break;
                        }

                        //检测PLC的MPR发送请求
                        var MprSendReq = (bool)Handle_Plc.Read(Item_Reference, "Bool");

                        if (!MprSendReq)
                        {
                            goto Start;
                        }

                        //
                        var MprName = Handle_Plc.Read(Item_OrderState, "String").ToString();

                        if (!string.IsNullOrWhiteSpace(MprName))
                        {
                            goto Start;
                        }

                        //PLC反馈接收到了任务，修改任务状态并作历史记录
                        Handle_Plc.Write(Item_OrderId, MprName);

                        //Handle_Modify.ModifyTransState(t_TransOnlineRecords);

                        goto Start;
                    }
                }
                catch (Exception ex)
                {

                }
            } while (true);
        }
    }
}

